package com.jingdianjichi.subject.handle.subject;

import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目工厂类
 */
@Component
public class SubjectTypeHandleFactory implements InitializingBean {

    Map<SubjectInfoTypeEnum, SubjectTypeHandler> handlerMap = new HashMap();
    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlers;

    public SubjectTypeHandler getSubjectTypeHandler(int subjectType) {
        SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
        return handlerMap.get(subjectInfoTypeEnum);
    }

    /**
     * 盲猜是在bean初始化后给属性赋值
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlers) {
            handlerMap.put(subjectTypeHandler.getHandleType(), subjectTypeHandler);
        }
    }
}
